$( document ).on( "pageshow", "#guessWhoFlower", function( event ) {
	var height=document.getElementById('guessKingResultArea').offsetTop+1;
	$("#wrapper_guessWhoFlower").css({"top":height+"px"});
	
});
var searchCont_guessWhoFlower="";
var isCanGuessKing_guessWhoFlower=true;
$( document ).on( "pagecreate", "#guessWhoFlower", function( event ) {
	hideWXToolBar();
	access_token=getQueryString("access_token");
	
	$("#wrapper_guessWhoFlower  #pullDown").hide();
	pageNo="1";
	pageSize="10";
	totalPage=0;
	var scrollObj_guessWhoFlower=scrollPlugLoadedOfReturn("wrapper_guessWhoFlower",
			function() {
				setTimeout(function () {
					if(pageNo >= totalPage)
						return;
					pageNo++;
					loadData_guessWhoFlower(false,scrollObj_guessWhoFlower);
				}, 1000);	
			},
			function() {
//				setTimeout(function () {
//					pageNo=1;
//					loadData_guessWhoFlower(true);
//				}, 1000);
				scrollObj_guessWhoFlower.refresh();
			});
	
	loadData_guessWhoFlower(true,'',scrollObj_guessWhoFlower);
	//做搜索(方法体待添加)
	$("#searchBtn_guessWhoFlower").on("click",function (){
			pageNo="1";
			pageSize="10";
			searchCont_guessWhoFlower=$("#searchCon_guessWhoFlower").val();
			loadData_guessWhoFlower(true,searchCont_guessWhoFlower,scrollObj_guessWhoFlower);
	});
});
/**
 * 
 * @param isFresh 是否刷新
 * @param isFresh 是否初始列表
 */
function loadData_guessWhoFlower(isFresh,content,scrollObj_guessWhoFlower){
	var param="";
	if(content==undefined || content=="")
		param="access_token="+access_token+"&pageNo="+pageNo+"&pageSize="+pageSize;
	else
		param="access_token="+access_token+"&pageNo="+pageNo+"&pageSize="+pageSize+"&content="+content;
	
	$.ajax({
	  	url: rootPath+"/guessKing/searchYesTodayFlowerRank.do",
	  	type: "POST",
	  	dataType: "text",
		data:param ,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				if(!isNullOrEmpty(data.msg)){
					if(data.msg=="true"){
						isCanGuessKing_guessWhoFlower=true;
					}else{
						isCanGuessKing_guessWhoFlower=false;
					}
				}
				totalPage=data.data.totalPage;
		  		var totalRow=data.data.totalRow;
		  		var arr=data.data.rows;
				var content = "";
				for (var i=0;i<arr.length;i++){
					var headerPic=rootPath+defaultHeadPic;
					if(arr[i].addManHeadImgUrl!=undefined && arr[i].addManHeadImgUrl!="")
						headerPic=arr[i].addManHeadImgUrl;
					var flowerUrl=rootPath+flower_bloom;
					var a_click="guessKingIsWho('"+arr[i].addManId+"','"+arr[i].addManName+"')";
					content +='<li>\
					  <a href="#" onClick="'+a_click+'">\
					  	<span class="ui-li-desc li_one ">'+(arr[i].id)+'</span>\
					  	<span class="ui-li-desc li_two "><img src="'+headerPic+'"  width="40" height="40"></span>\
						<span class="ui-li-desc li_three " >'+(arr[i].addManName)+'</span>\
						<span class="ui-li-desc li_four " ><img src="'+flowerUrl+'" width="40" height="40"/></span>\
						<span class="ui-li-desc li_five " ><strong>'+parseInt(arr[i].num)+'</strong></span>\
					  </a>\
					</li>';
				}
				if(arr.length<pageSize){
					$("#wrapper_guessWhoFlower  #pullUp").hide();
				}else{
					$("#wrapper_guessWhoFlower  #pullUp").show();
				}
				if(isFresh && $("#thelist_guessWhoFlower").html()!=""){
					$("#thelist_guessWhoFlower").html("");
				}
				$("#thelist_guessWhoFlower").append(content).listview('refresh');
				scrollObj_guessWhoFlower.refresh();
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}

function guessKingIsWho(userId,userName){
	//如果已经押过宝了
	if(!isCanGuessKing_guessWhoFlower){
		alert_wx('fail',"押宝机会已用完，请明天再来吧！");
		return false;
	}
	$.lt_alert({
		type: 'confirm',
		right_close:false,
		content: '亲，您确定[' + userName + ']会成为今天的红花王吗？',
		first_but:'确定',
		first_callBack:function (){
			//调用押宝接口，进行押宝
			startGuessKing(userId);
			$.lt_alert_close();
		},
		second_but:'取消',
		second_callBack:function (){
			setTimeout(function (){
					$.lt_alert_close();
			},500);	
		}
	});
}

function startGuessKing(userId,userName){
	var param="access_token="+access_token+"&empId="+userId;
	$.ajax({
	  	url: rootPath+"/guessKing/bettingOfRedFlower.do",
	  	type: "POST",
	  	dataType: "text",
		data:param ,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				isCanGuessKing_guessWhoFlower=false;
				if(data.msg==undefined || data.msg==null || data.msg=="")
					alert_wx('success',"亲，您认定的花王已成功提交，敬请期待明天的结果吧！",1000);
				else
					alert_wx('success',data.msg,5000);	
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}
///**
// * 初始化iScroll控件
// */
//function loaded2() {
//	if(myScroll!=null){
//		myScroll.destroy();
//	}
//	myScroll = new iScroll('wrapper_guessWhoFlower', {
//		scrollbarClass: 'myScrollbar'
//	});
//	
//	setTimeout(function () { document.getElementById('wrapper_guessWhoFlower').style.left = '0'; }, 800);
//}
